package com.ccom.test.serviceImpl;

import com.ccom.core.common.model.BaseMap;
import com.ccom.redis.util.RedisUtil;
import com.ccom.starter.rabbitmq.client.RabbitMqClient;
import com.ccom.test.constant.TestConstant;
import com.ccom.test.mapper.ArticleMapper;
import com.ccom.test.service.RabbitMqTestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description:
 * @author: jjzhang24
 * @date: 2021年12月17日
 */
@Slf4j
@Service
public class RabbitMqTestServiceImpl implements RabbitMqTestService {

    @Autowired
    private RabbitMqClient rabbitMqClient;

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private RabbitAdmin rabbitAdmin;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public void sendMassage(String queue, String message) {
        BaseMap map = new BaseMap();
        map.put("message", message);
        rabbitMqClient.sendMessage(queue, map);
    }

    @Override
    public void sendMassage(String queue, String message, Integer time) {
        BaseMap map = new BaseMap();
        map.put("message", message);
        rabbitMqClient.sendMessage(queue, map, time);
    }

    @Override
    public void publishEvent(String message) {
        BaseMap map = new BaseMap();
        map.put("message", message);
        rabbitMqClient.publishEvent(TestConstant.MQ_DEMO_BUS_EVENT, map);
    }

    @Override
    public String receiveFromQueue(String queueName) {
        return rabbitMqClient.receiveFromQueue(queueName);
    }

    @Override
    public boolean subcribe(String articleId, String userId) {
        String autherId = articleMapper.findById(articleId).getUserId().toString();
        DirectExchange directExchange = new DirectExchange(TestConstant.CCOM_ARTICLE_SUBCRIBE_EXCHANGE);
        rabbitAdmin.declareExchange(directExchange);
        Queue queue = new Queue(TestConstant.CCOM_ARTICLE_SUBCRIBE_QUEUE_PRIFIX + userId, true);
        Binding binding = BindingBuilder.bind(queue).to(directExchange).with(autherId);
        String userKey = "article_subcribe_" + userId;
        String authorKey = "article_author_" + autherId;
        if (redisUtil.sGet(userKey).contains(autherId)) {
            redisUtil.setRemove(userKey, autherId);
            redisUtil.setRemove(authorKey, userId);
            rabbitAdmin.removeBinding(binding);
            return false;
        }
        //用户关注作者，作者粉丝添加用户
        redisUtil.sSet(userKey, autherId);
        redisUtil.sSet(authorKey, userId);
        rabbitAdmin.declareQueue(queue);
        rabbitAdmin.declareBinding(binding);
        log.info("绑定成功，directExchange:{},queue:{}", TestConstant.CCOM_ARTICLE_SUBCRIBE_EXCHANGE, TestConstant.CCOM_ARTICLE_SUBCRIBE_QUEUE_PRIFIX + userId);
        return true;
    }
}
